package cn.cena.org.service.impl;

import cn.cena.basic.service.impl.BaseServiceImpl;
import cn.cena.basic.util.PageList;
import cn.cena.org.domian.Department;
import cn.cena.org.mapper.DepartmentMapper;
import cn.cena.org.query.DepartmentQuery;
import cn.cena.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void add(Department department) {
        departmentMapper.save(department);
        // 更新
        update(department); // 调用本类更新方法
    }

    @Override
    public void update(Department department) {
        if (department.getParent() == null || department.getParent().getId() == null){ // 父级部门
            department.setDirPath("/" + department.getId()); // 设置dirPath
        } else { // 不是父级部门
            Department parentDept = departmentMapper.findOne(department.getParent().getId()); // 找到父级部门
            String parentDeptDirPath = parentDept.getDirPath(); // 找到父级部门dirPath
            department.setDirPath(parentDeptDirPath + "/" + department.getId());
        }
        departmentMapper.update(department);
    }

    @Override
    public List<Department> parentTree() {
        return departmentMapper.parentTree();
    }
}
